package com.itheima.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.domain.Emp;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class EmpController {
    @RequestMapping("/findOne")
    public ModelAndView findOne(ModelAndView modelAndView){
        // ModelAndView mav = new ModelAndView();  可以替换参数中的ModelAndView
        // 添加数据
        Emp emp = new Emp();
        emp.setAge(25);
        emp.setName("陈彪");
        modelAndView.addObject("emp",emp);
        // 跳转页面
        modelAndView.setViewName("WEB-INF/page.jsp");
        return modelAndView;
    }

    @RequestMapping("/jsonData")
    @ResponseBody
    public String jsonData() throws JsonProcessingException {
        // ModelAndView mav = new ModelAndView();  可以替换参数中的ModelAndView
        // 添加数据
        Emp emp = new Emp();
        emp.setAge(25);
        emp.setName("陈彪");

        // 转json对象
        ObjectMapper om = new ObjectMapper();
        return om.writeValueAsString(emp);
    }

    @RequestMapping("/jsonData2")
    @ResponseBody
    public Emp jsonData2(){
        // ModelAndView mav = new ModelAndView();  可以替换参数中的ModelAndView
        // 添加数据
        Emp emp = new Emp();
        emp.setAge(25);
        emp.setName("陈彪");
        return emp;
    }
}
